import { Injectable } from '@nestjs/common';
import { UserModel } from "../Scheam/user.scheam"
import type { User_add } from "../Types/User"
@Injectable()
export class UserService {
  async create(user_data: User_add) {
    try {
      const result = await UserModel.create({
        ...user_data
      })
      return result
    } catch (error) {
      console.log(error)
      return false

    }
  }

  async login({ username, password }) {
    try {
      const result = await UserModel.findOne({ username, password }).select('-password -__v -role -qqnumber')
      return result
    } catch (error) {

      return null

    }
  }
  async login_admin({ username, password }) {
    try {
      const result = await UserModel.findOne({ username, password }).select('username avatar role')
      return result
    } catch (error) {

      return null

    }
  }
  async forget({ qqnumber, password }) {
    return await UserModel.findOneAndUpdate({ qqnumber }, { password: password })

  }
  async update(id, data) {
    try {
      return await UserModel.updateOne({ _id: id }, { ...data })
    } catch (error) {
      return {
        matchedCount: 0
      }
    }

  }
  async update_pass({ oldpassword, password }) {
    try {
      return await UserModel.updateOne({ password: oldpassword }, { password: password })
    } catch (error) {
      console.log(error)
      return false
    }
  }
  async findAll(skip = 0, role = "3", keyword = "") {
    try {
      if (!keyword) {
        if (role === "3") {
          return {
            data: await UserModel.find().sort({ 'brithday': "desc" }).skip(skip).limit(5).select('-description -password -__v  -bilibili'),
            count: await UserModel.aggregate([
              {
                $count: "total"
              }
            ])
          }
        } else {
          return {
            data: await UserModel.find({
              role: role
            }).sort({ 'brithday': "desc" }).skip(skip).limit(5).select('-description -password -__v  -bilibili'),
            count: await UserModel.aggregate([
              {
                $match: {
                  role: role
                }
              },
              {
                $count: "total"
              }
            ])
          }
        }
      } else {
        if (role === "3") {
          return {
            data: await UserModel.find({
              username: { $regex: keyword, $options: 'i' }
            }).sort({ 'brithday': "desc" }).skip(skip).limit(5).select('-description -password -__v  -bilibili'),
            count: await UserModel.aggregate([
              {
                $match: {
                  username: { $regex: keyword, $options: 'i' }
                }
              },
              {
                $count: "total"
              }
            ])
          }
        } else {
          return {
            data: await UserModel.find({
              role: role,
              username: { $regex: keyword, $options: 'i' }
            }).sort({ 'brithday': "desc" }).skip(skip).limit(5).select('-description -password -__v  -bilibili'),
            count: await UserModel.aggregate([
              {
                $match: {
                  role: role,
                  username: { $regex: keyword, $options: 'i' }
                }
              },
              {
                $count: "total"
              },

            ])
          }
        }
      }
    } catch (error) {
      console.log(error)
      return false
    }
  }
  async updateRole(id, role) {
    try {
      return UserModel.updateOne({ _id: id }, { role: role })
    } catch (error) {
      return false
    }
  }

  async deleteOne(id) {
    try {
      return UserModel.deleteOne({ _id: id })
    } catch (error) {
      return false
    }
  }
}
